using System;
using System.Collections.Generic;
using System.Data;
using ud_Poll.Data;

namespace ud_Poll.Business
{
    /// <summary>
    /// Represents an instance of a polloption
    /// </summary>
    public class PollOption
    {
        #region Constructors


        public PollOption()
        { }

        public PollOption(Guid optionGuid)
        {
            if (optionGuid != Guid.Empty)
            {
                GetPollOption(optionGuid);
            }
        }


        #endregion

        #region Private Properties

        private Guid pollGuid = Guid.Empty;
        private Poll poll;
        private Guid optionGuid = Guid.Empty;
        private String answer = String.Empty;
        private int votes;
        private int order = 1;
        private readonly Guid newOptionGuid = Guid.NewGuid();


        #endregion

        #region Public Properties

        public Guid PollGuid
        {
            get { return pollGuid; }
            set { pollGuid = value; }
        }

        public Guid OptionGuid
        {
            get { return optionGuid; }
        }

        public String Answer
        {
            get { return answer; }
            set { answer = value; }
        }

        public int Votes
        {
            get { return votes; }
        }

        public double? VotePercentage
        {
            get
            {
                if (Poll.TotalVotes == 0) return null;

                return votes / (double)Poll.TotalVotes * 100;
            }
                
        }

        public int Order
        {
            get { return order; }
            set { order = value; }
        }

        public Poll Poll
        {
            get { return poll ?? (poll = new Poll(pollGuid)); }
        }

        #endregion


        #region Private Methods

        private void GetPollOption(Guid option)
        {
            using (IDataReader reader = DBPollOption.GetPollOption(option))
            {
                if (reader.Read())
                {
                    pollGuid = new Guid(reader["PollGuid"].ToString());
                    answer = reader["Answer"].ToString();
                    order = int.Parse(reader["Order"].ToString());
                    votes = int.Parse(reader["Votes"].ToString());

                    optionGuid = option;
                }
            }
        }

        private bool Create()
        {
            optionGuid = newOptionGuid;

            int rowsAffected = DBPollOption.Add(
                optionGuid,
                pollGuid, 
                answer,
                order);

            return (rowsAffected > 0);
        }

        private bool Update()
        {
            return DBPollOption.Update(
                optionGuid, 
                answer,
                order);
        }

        #endregion


        #region Public Methods

        public bool Save()
        {
            if (optionGuid == Guid.Empty) return Create();
            return Update();
        }

        public bool IncrementVotes(Guid userGuid)
        {
            if (optionGuid == Guid.Empty) return false;

            return DBPollOption.IncrementVotes(pollGuid, optionGuid, userGuid);
        }

        public bool Delete()
        {
            if (optionGuid == Guid.Empty) return false;

            return DBPollOption.Delete(optionGuid);
        }

        #endregion

        #region Static Methods

        private static List<PollOption> LoadListFromReader(IDataReader reader)
        {
            List<PollOption> pollOptionList = new List<PollOption>();
            try
            {
                while (reader.Read())
                {
                    PollOption pollOption = new PollOption
                                                {
                                                    optionGuid = new Guid(reader["OptionGuid"].ToString()),
                                                    pollGuid = new Guid(reader["PollGuid"].ToString()),
                                                    answer = reader["Answer"].ToString(),
                                                    votes = Convert.ToInt32(reader["Votes"]),
                                                    order = Convert.ToInt32(reader["Order"])
                                                };
                    pollOptionList.Add(pollOption);

                }
            }
            finally
            {
                reader.Close();
            }

            return pollOptionList;

        }

        public static List<PollOption> GetOptionsByPollGuid(Guid pollGuid)
        {
            IDataReader reader = DBPollOption.GetPollOptions(pollGuid);
            return LoadListFromReader(reader);
        }

        #endregion

    }
}
